﻿using System;
using System.Windows;
using System.Windows.Data;

namespace Kaesoft.Hospedaje.Common
{
    /// <summary>
    /// Convierte de bool a Visible
    /// </summary>
    public class VisibleConverter : IValueConverter
    {
        #region IValueConverter Members
        /// <summary>
        /// Convertir de bool a System.Windows.Visibility
        /// </summary>
        /// <param name="value">Valor bool</param>
        /// <param name="targetType">Tipo de retorno</param>
        /// <param name="parameter">Parametro de conversion</param>
        /// <param name="culture">Cultura actual</param>
        /// <returns>Valor convertido a Visibility</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                bool val = (bool)value;
                if (parameter == null)
                {
                    if (val)
                        return Visibility.Visible;
                }
                else
                {
                    if (val)
                        return Visibility.Collapsed;
                    else
                        return Visibility.Visible;
                }
            }
            else if (value is string)
            {
                if(!String.IsNullOrEmpty(value.ToString().Trim()))
                     return Visibility.Visible;
            }
            return Visibility.Collapsed;
        }

        /// <summary>
        /// Conversión de Visibility a bool
        /// </summary>
        /// <param name="value">Valor tipo Visibility</param>
        /// <param name="targetType">Tipo de retorno (bool)</param>
        /// <param name="parameter">Parámetro de conversion</param>
        /// <param name="culture">Cultura actual</param>
        /// <returns>Valor convertido a bool</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Visibility)
            {
                Visibility val = (Visibility) value;
                if (parameter == null)
                {
                    if (val == Visibility.Visible)
                        return true;
                }
                else
                {
                    if (val == Visibility.Visible)
                        return false;
                    else
                        return true;
                }
            }
            return false;
        }
        #endregion
    }
}
